package com.cloud.bc.controller.ajax.center;

import cn.hutool.core.convert.Convert;
import com.cloud.bc.model.domain.UserInfo;
import com.cloud.bc.model.entity.BcChats;
import com.cloud.bc.model.entity.BcGreespeaks;
import com.cloud.bc.model.entity.BcGreets;
import com.cloud.bc.model.entity.BcSingles;
import com.cloud.bc.model.request.GreetsRequest;
import com.cloud.bc.model.response.AjaxResp;
import com.cloud.bc.model.response.MatchedResponse;
import com.cloud.bc.service.*;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@AllArgsConstructor
@RestController
@RequestMapping("m/ajax/center/greet")
public class Ajax_GreetController {
    private final BcGreespeaksService bcGreespeaksService;
    private final BcUsersService bcUsersService;
    private final BcSinglesService bcSinglesService;
    private final BcGreetsService bcGreetsService;
    private final BcChatsService bcChatsService;

    /**
     * 打招呼
     */
    @GetMapping("")
    public AjaxResp greet(HttpServletRequest request,
                          HttpServletResponse response
    ) {
        //默认是查询全部
        String sex = "0";
        UserInfo userInfo = bcUsersService.getUserInfo(request);
        if (null != userInfo && Convert.toInt(userInfo.getId()) > 0) {
            BcSingles bcSinglesReq = new BcSingles();
            bcSinglesReq.setUserId(userInfo.getId());
            BcSingles bcSingles = bcSinglesService.selectOne(bcSinglesReq);
            Integer gender = Convert.toInt(bcSingles.getGender(), 0);
            if (gender != 0) {
                gender = gender == 1 ? 2 : 1;
                sex = 0 + "," + gender;
            }
        }
        List<BcGreespeaks> bcGreespeaksList = bcGreespeaksService.selectAll(sex);
        StringBuilder sb = new StringBuilder();
        sb.append("<div class=\"bcui-form js-pop-greet\" style=\"padding:0\"><div class=\"form-item ub\" style=\"margin-top:.5rem;\"><select name=\"greet_id\" style=\"border-radius:3px\">");
        sb.append("<option value=\"\">选择招呼语</option>");
        bcGreespeaksList.forEach(item -> {
            sb.append("<option value=\"" + item.getId() + "\">" + item.getSpeak() + "</option>");
        });
        sb.append("</div></div><div class=\"text\" style=\"margin-top:.2rem;font-size:.7rem;color:#999\">打招呼显示在聊天窗口中.</div>");

        AjaxResp ajaxResp = new AjaxResp();
        ajaxResp.setObj(sb.toString());
        return ajaxResp;
    }

    /**
     * 打招呼
     */
    @PostMapping("send")
    public AjaxResp send(HttpServletRequest request,
                         HttpServletResponse response,
                         GreetsRequest req
    ) {
        AjaxResp ajaxResp = new AjaxResp();
        UserInfo userInfo = bcUsersService.getUserInfo(request);
        //是否是批量打招呼
        String[] target_ids = request.getParameterValues("target_ids[]");
        if (null != target_ids && target_ids.length > 0) {
            //批量打招呼
            for (String target_id : target_ids) {
                BcChats bcChats = new BcChats();
                bcChats.setTargetId(Convert.toInt(target_id));
                bcChats.setUserId(userInfo.getId());
                bcChats.setWords("你好，能认识一下吗？");
                bcChatsService.save(bcChats);
                //记录聊天
            }
            return ajaxResp;
        }
        BcGreets bcGreets = new BcGreets();
        bcGreets.setUserId(userInfo.getId());
        bcGreets.setTargetId(req.getTargetId());
        int greetCount = bcGreetsService.count(bcGreets);
        if (greetCount > 0) {
            ajaxResp.setSuccess(false);
            ajaxResp.setCode(43404);
            ajaxResp.setMsg("已经打过招呼了～");
            return ajaxResp;
        }
        BcGreespeaks byId = bcGreespeaksService.getById(req.getId());
        bcGreets.setSpeak(byId.getSpeak());
        bcGreetsService.save(bcGreets);
        //记录聊天
        BcChats bcChats = new BcChats();
        bcChats.setTargetId(req.getTargetId());
        bcChats.setUserId(userInfo.getId());
        bcChats.setWords(byId.getSpeak());
        bcChatsService.save(bcChats);
        //记录聊天
        return ajaxResp;
    }

    //批量打招呼
    @GetMapping("matched")
    public AjaxResp matched(HttpServletRequest request, HttpServletResponse response) {
        AjaxResp ajaxResp = new AjaxResp();
        List<MatchedResponse> list = bcGreetsService.matched();
        ajaxResp.setList(list);
        return ajaxResp;

    }
}
